
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;

using Swaf;

namespace Swaf.BizRule
{
    public enum RuleExtensionType {Pre=0, Post=1};
    public class BizRuleExtensionDecorator : GenericDecorator, IRuleDecorator
    {
        protected IList[] m_ruleExtensions = new IList[2]; //2 matches up to the num elements in the RuleExtensionType enum

        public BizRuleExtensionDecorator(string name, string val, object extraInfo)
            :base(name, val, extraInfo)
        {
        }

        public void addRuleExtension(RuleExtensionType type, string extensionRuleName)
        {
            if (m_ruleExtensions[(int)type] == null)
                m_ruleExtensions[(int)type] = new ArrayList();

            m_ruleExtensions[(int)type].Add(extensionRuleName);
        }
        #region IRuleDecorator Members

        public void postRuleExecution(IRunningRule info, ref object ruleReturn, ref Exception ruleException)
        {
            IResults rc;
			if (m_ruleExtensions[(int)RuleExtensionType.Post] != null && ruleException == null)
			{
				IRuleContext context = (IRuleContext)info.inputParams[1];
				foreach (string ruleName in m_ruleExtensions[(int)RuleExtensionType.Post])
				{

					rc = Application.currentApp.ruleMgr.execute(context, ruleName, ruleReturn, info.inputParams);
					ruleReturn = rc.CallResults;
					ruleException = rc.ExceptionInfo;
				}
			}
        }

        public bool preRuleExecution(IRunningRule info)
        {
            IResults rc = null;
			if (m_ruleExtensions[(int)RuleExtensionType.Pre] != null)
			{
				IRuleContext context = (IRuleContext)info.inputParams[1];
				foreach (string ruleName in m_ruleExtensions[(int)RuleExtensionType.Pre])
					rc = Application.currentApp.ruleMgr.execute(context, ruleName, info.inputParams);
			}
            return (rc == null ||(rc != null && rc.ExceptionInfo == null));
        }

        public override bool IsStandin {get{return true;}}

        #endregion
    }

    public class BizRuleExtendorDecorator : GenericDecorator
    {
        public BizRuleExtendorDecorator(string name, string val, object extraInfo)
            : base(name, val, extraInfo)
        {
            IRuleManagerEx ruleMgr = Application.globalApp.ruleMgr as IRuleManagerEx;
            BasicRuleInfo ruleInfo = ruleMgr.getRuleInfo(val,false) as BasicRuleInfo;
            IRuleInfo onRuleInfo = extraInfo as IRuleInfo;
            if (ruleInfo != null && onRuleInfo != null)
            {
                RuleExtensionType extType = RuleExtensionType.Post;
                if(name.ToLower().StartsWith("pre"))
                    extType = RuleExtensionType.Pre;
                BizRuleExtensionDecorator extDeco = ruleInfo.Decorators["Extensions"] as BizRuleExtensionDecorator;
                if (extDeco == null)
                {
                    extDeco = new BizRuleExtensionDecorator("Extensions", "", null);
                    ruleInfo.addDecorator(extDeco);
                }
                extDeco.addRuleExtension(extType, onRuleInfo.Name);
            }
        }
    }
}
